package ogmios.components;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 * A composite of the split pane and tabbed pane. It will
 * only be one (either split or tabbed) at a time.
 */
public class SplitTabPane extends JPanel
{
 private Component left;
 private Component right;
 private String leftName;
 private String rightName;
 private boolean isSplit = false;
 private JSplitPane splitter;
 private JTabbedPane tabs;

 /**
 * Creates a SplitTabPane with no left or right component, and horizontally split
 */
 public SplitTabPane()
 {
  this(null, null);
 }
 
 /**
 * Create a SplitTabPane with a left and right component, and horizontally split
 * @param     left     The component on the left
 * @param     right     The component on the right
 */
 public SplitTabPane(Component left, Component right)
 {
  this(JSplitPane.HORIZONTAL_SPLIT, left, right);
 }

 /**
 * Creates a SplitTabPane with no components
 * @param     orientation     The orientation of the split. See JSplitPane for orientation constants.
 */
 public SplitTabPane(int orientation)
 {
  this(orientation, null, null);
 }

 /**
 * Create a SplitTabPane with a left and right component, and split according to the passed argument
 * @param     orientation     The orientation of the split. See JSplitPane for orientation constants.
 * @param     left     The component on the left
 * @param     right     The component on the right
 */
 public SplitTabPane(int orientation, Component left, Component right)
 {
  this(orientation, left, right, false);
 }
 /**
 * Create a SplitTabPane with a left and right component, and split according to the passed argument
 * @param     orientation     The orientation of the split. See JSplitPane for orientation constants.
 * @param     left     The component on the left
 * @param     leftName     The name to display with the left component
 * @param     right     The component on the right
 * @param     rightName     The name to display with the right component
 */
 public SplitTabPane(int orientation, Component left, String leftName, Component right, String rightName)
 {
  this(orientation, left, leftName, right, rightName, false);
 } 
 /**
 * Create a SplitTabPane with a left and right component, and split according to the passed argument
 * @param     orientation     The orientation of the split. See JSplitPane for orientation constants.
 * @param     left     The component on the left
 * @param     right     The component on the right
 * @param     split     Whether the SplitTabPane opens up initially in Split mode
 */
 public SplitTabPane(int orientation, Component left, Component right, boolean split)
 {
  this(orientation, left,  left == null ? "Left" : left.getName(), right, right==null ? "Right" :right.getName(), split); 
 }

 /**
 * Create a SplitTabPane with a left and right component, and split according to the passed argument
 * @param     orientation     The orientation of the split. See JSplitPane for orientation constants.
 * @param     left     The component on the left
 * @param     leftName     The name to display with the left component
 * @param     right     The component on the right
 * @param     rightName     The name to display with the right component
 * @param     split     Whether the SplitTabPane opens up initially in Split mode
 */
 public SplitTabPane(int orientation, Component left, String leftName, Component right, String rightName, boolean split)
 {
   super(new BorderLayout());
   tabs = new JTabbedPane();
   splitter = new JSplitPane(orientation);
   isSplit = !split;  
   splitTabs(split);
   setLeftComponent(left);
   setRightComponent(right);
 }

 /**
 * Returns whether the SplitTabPane is in split mode
 */
 public boolean isSplit()
 {
  return isSplit;
 }

 /**
 * Changes to either split or tab mode (if you set it to the mode it is already in, nothing happens)
 */
 public void splitTabs(boolean split)
 {
  if (split)
  {
    if (!isSplit)
    {
      remove(tabs);
      if (left != null)
       tabs.remove(left);
      if (right != null)
       tabs.remove(right);
      splitter.add(left);
      splitter.add(right);
      add(splitter);
      validate();
      splitter.setDividerLocation(0.5);
      repaint();
    }
  }
  else
  {
    if (isSplit)
    {
      remove(splitter);
      if (left != null)
       splitter.remove(left);
      if (right != null)
       splitter.remove(right);
      tabs.add(leftName, left);
      tabs.add(rightName, right);
      add(tabs);
      validate();
      repaint();
    }
  }
  isSplit = split;
 }

 /**
 * Set component on the left (or top) of the split tabbed pane. Does nothing if not in split mode.
 */
 public void setLeftComponent(Component left)
 {
  setLeftComponent(left, left==null ? "Left" : left.getName());
 }

 /**
 * Set component on the right (or bottom) of the split tabbed pane. Does nothing if not in split mode.
 */
 public void setRightComponent(Component right)
 {
  setRightComponent(right, right==null ? "Right" : right.getName());
 }
 /**
 * Set component on the left (or top) of the split tabbed pane. Does nothing if not in split mode.
 */
 public void setLeftComponent(Component left, String leftName)
 {
  if (isSplit)
  {
   splitter.setLeftComponent(left);
  }
  else
  {
   tabs.remove(this.left);
   tabs.insertTab(leftName, null, left, null, 0);
  }
  this.left = left;
  this.leftName = leftName;
  validate();
  repaint();
 }
 /**
 * Set component on the right (or bottom) of the split tabbed pane. Does nothing if not in split mode.
 */
 public void setRightComponent(Component right, String rightName)
 {
  if (isSplit)
  {
   splitter.setRightComponent(right);
  }
  else
  {
   tabs.remove(this.right);
   tabs.addTab(rightName, right);
  }
  this.right = right;
  this.rightName = rightName;
  validate();
  repaint();
 }
 /**
 * Sets the selected tab index. Does nothing if in split mode.
 */
 public void setSelectedIndex(int index)
 {
   if (!isSplit)
   {
    tabs.setSelectedIndex(index);
   }
 }
 /**
 * Sets the selected tab component. Does nothing if in split mode.
 */
 public void setSelectedComponent(boolean left)
 {
  if (!isSplit)
   tabs.setSelectedIndex(left ? 0 : 1);
 }

 /**
 * Returns the underlying JSplitTabPane
 */
 public JSplitPane getSplitPane()
 {
  return splitter;
 }

 /**
 * Returns the underlying JTabbedPane
 */
 public JTabbedPane getTabbedPane()
 {
  return tabs;
 }

}
